package info.woodcock.security.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
    /**
     * create two datasource; mysql and oracle
     * @return
     */

    @Bean(name="oracledatasource")
    @ConfigurationProperties("spring.datasource.druid.oracle")
    public DataSource dataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name="mysqldatasource")
    @ConfigurationProperties("spring.datasource.druid.mysql")
    public DataSource dataSourceMySql(){
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * To create two JDBCTemplate instance
     */
    @Bean(name="mysqlJDBCTemplate")
    public JdbcTemplate mysqlJDBCTemplate(@Qualifier(value = "mysqldatasource")DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    @Bean(name="oracleJDBCTemplate")
    public JdbcTemplate oracleJDBCTemplate(@Qualifier(value = "oracledatasource")DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

}
